#include "linkedlist.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

int InitialLinkedList(LinkedList l)
{
	l->head = NULL;
	l->length = 0;

};

int InsertLinkedList(LinkedList l, int index, int item)
{
	if (index > l->length)
	{
		return ERR;
	}
	struct LinkedListNode *node = malloc(sizeof(struct LinkedListNode));
	if (!node)
	{
		return ERR;
	}
	node->item = item;
	if (index == 0)
	{
		node -> next = l->head;
		l->head = node;
		l->length++;
		return OK;
	}

	int i = 0;
	struct LinkedListNode *p = l->head;
	while (i < index - 1)
	{
		p = p->next;
		i++;
	}

	node->next = p->next;
	p->next = node;
	l->length++;
	return OK;
}

void PrintLinkedList(LinkedList l)
{
	if (l -> length == 0)
	{
		printf("[]\n");
		return;
	}
	printf("[");
	struct LinkedListNode *p = l->head;
	while (p)
	{
		printf("%d ",p->item);
		p = p->next;
	}
	printf("]\n");
}
